package com.kzd.sdk.ui.fragment;

import android.text.TextUtils;
import android.view.View;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.kzd.sdk.base.OwnerFragment;
import com.kzd.sdk.callback.KZD_HttpCallback;
import com.kzd.sdk.callback.KZD_OnItemChildClickListener;
import com.kzd.sdk.core.KZD_ApiManager;
import com.kzd.sdk.entity.KZD_GameGift1;
import com.kzd.sdk.ui.adapter.KZD_GameGift1Adapter;
import com.kzd.sdk.utils.AppUtils;

import java.util.List;

public class KZD_HomeGiftFragment extends OwnerFragment {

    private RecyclerView mRecyclerView;
    private SwipeRefreshLayout mSwipeRefreshLayout;

    private KZD_GameGift1Adapter mGameGiftAdapter;

    private final KZD_HttpCallback<List<KZD_GameGift1>> callbackGameGift = new KZD_HttpCallback<List<KZD_GameGift1>>() {
        @Override
        public void onSuccess(List<KZD_GameGift1> data) {
            mGameGiftAdapter.setNewInstance(data);
            mSwipeRefreshLayout.setRefreshing(false);
        }

        @Override
        public void onError(String error) {
            toast(error);
            mSwipeRefreshLayout.setRefreshing(false);
        }
    };

    private final KZD_HttpCallback<String> callbackDrawGift = new KZD_HttpCallback<String>() {
        @Override
        public void onSuccess(String data) {
            initData();
            toast("领取成功");
        }

        @Override
        public void onError(String error) {
            toast(error);
        }
    };

    @Override
    public String getLayoutIdByName() {
        return "kzd_include_app_recycler";
    }

    @Override
    public void initView(View view) {
        mRecyclerView = view.findViewById(AppUtils.getId(mContext, "mRecyclerView"));
        mGameGiftAdapter = new KZD_GameGift1Adapter(mContext);
        mGameGiftAdapter.setOnItemChildClickListener(new KZD_OnItemChildClickListener() {
            @Override
            public void onClick(View v, int position) {
                String id = mGameGiftAdapter.get(position)
                        .getId();
                if (TextUtils.isEmpty(id)) {
                    return;
                }
                KZD_ApiManager.httpDrawGift(id, callbackDrawGift);
            }
        });
        mRecyclerView.setLayoutManager(new LinearLayoutManager(mContext));
        mRecyclerView.setAdapter(mGameGiftAdapter);

        mSwipeRefreshLayout = view.findViewById(AppUtils.getId(mContext, "mSwipeRefreshLayout"));
        mSwipeRefreshLayout.setColorSchemeResources(AppUtils.getColorId(mContext,"kzd_2983ff"));
        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                initData();
            }
        });
    }

    @Override
    public void initData() {
        KZD_ApiManager.httpGameGift(callbackGameGift);
    }
}
